package com.gwnet.cabinet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwnet.cabinet.domian.CabinetBoxLog;
import com.gwnet.cabinet.domian.bo.CabinetBoxLogPageBO;
import com.gwnet.cabinet.mapper.CabinetBoxLogMapper;
import com.gwnet.cabinet.service.CabinetBoxLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 外卖柜-箱格表-日志表 服务实现类
 * </p>
 *
 * @author WangLiang
 * @since 2024-07-23
 */
@Service
public class CabinetBoxLogServiceImpl extends ServiceImpl<CabinetBoxLogMapper, CabinetBoxLog> implements CabinetBoxLogService {

    private final CabinetBoxLogMapper boxLogMapper;

    @Autowired
    public CabinetBoxLogServiceImpl(CabinetBoxLogMapper boxLogMapper) {
        this.boxLogMapper = boxLogMapper;
    }


    /**
     * 分页查询箱格存储历史
     * @param pageBO
     * @return
     */
    @Override
    public IPage<CabinetBoxLog> page(CabinetBoxLogPageBO pageBO) {
        Page page = Page.of(pageBO.getCurrent(),pageBO.getPageSize());
        LambdaQueryWrapper<CabinetBoxLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CabinetBoxLog::getDevSn,pageBO.getDevSn());
        // 日志表ID
        if(ObjectUtils.isNotEmpty(pageBO.getLogId())){
            queryWrapper.eq(CabinetBoxLog::getLogId,pageBO.getLogId());
        }

        // 学校ID
        if(ObjectUtils.isNotEmpty(pageBO.getSchoolId())){
            queryWrapper.eq(CabinetBoxLog::getSchoolId,pageBO.getSchoolId());
        }

        // 箱格ID
        if(ObjectUtils.isNotEmpty(pageBO.getBoxId())){
            queryWrapper.eq(CabinetBoxLog::getBoxId,pageBO.getBoxId());
        }

        // 箱格编号
        if(ObjectUtils.isNotEmpty(pageBO.getBoxNo())){
            queryWrapper.eq(CabinetBoxLog::getBoxNo,pageBO.getBoxNo());
        }

        // 订单ID
        if(ObjectUtils.isNotEmpty(pageBO.getOrderId())){
            queryWrapper.like(CabinetBoxLog::getOrderId,pageBO.getOrderId());
        }

        // 使用类型(1 存, 2 取, 3.维护)
        if(ObjectUtils.isNotEmpty(pageBO.getUseType())){
            queryWrapper.eq(CabinetBoxLog::getUseType, pageBO.getUseType());
        }

        // 密码/验证码
        if(ObjectUtils.isNotEmpty(pageBO.getCode())){
            queryWrapper.eq(CabinetBoxLog::getCode,pageBO.getCode());
        }

        Page resultPage = boxLogMapper.selectPage(page, queryWrapper);
        return resultPage;
    }

    @Override
    public CabinetBoxLog view(String logId) {
        CabinetBoxLog boxLog = boxLogMapper.selectById(logId);
        return boxLog;
    }
}
